import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soldado here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Soldado extends Enemigo
{
    
    private GreenfootImage [] soldierRight = new GreenfootImage[12];
    private GreenfootImage [] soldierLeft = new GreenfootImage[12];
    private GreenfootImage [] soldierAtakRight = new GreenfootImage[23];
    private GreenfootImage [] soldierAtakLeft= new GreenfootImage[23];
    private boolean ataque=false;
    private int direccion=-1;
    private int cont=0;
    private int tiempo=0;
    int delay=0;
    private int totalPasos=3;
    int pasos=0;
    
    /**
     * Act - do whatever the Soldado wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Soldado(int x0, int xf,int nivel)
    {
        super(3,x0,xf,nivel);
            for(int i=1; i<= soldierRight.length;i++)
    {
        soldierRight[i-1] = new GreenfootImage("rebeldecorre"+i+".png");
        soldierRight[i-1].scale(57,85);
        
    }
             for(int i=1; i<= soldierLeft.length;i++)
    {
        soldierLeft[i-1] = new GreenfootImage("rebeldecorre"+i+".png");
        soldierLeft[i-1].scale(57,85);
        soldierLeft[i-1].mirrorHorizontally();
        //soldierLeft[i-1].mirrorHorizontally();
    }

                for(int i=1; i<= soldierAtakRight.length;i++)
    {
        soldierAtakRight[i-1] = new GreenfootImage("rebeldesacacuchillo"+i+".png");
        soldierAtakRight[i-1].scale(57,85);
        
    }
             for(int i=1; i<= soldierAtakLeft.length;i++)
    {
        soldierAtakLeft[i-1] = new GreenfootImage("rebeldesacacuchillo"+i+".png");
        soldierAtakLeft[i-1].scale(57,85);
        soldierAtakLeft[i-1].mirrorHorizontally();
   
    }
    setImage(soldierAtakLeft[0]);
    }
    public void act()
    {
         //super.act();
        if(ataque)
        {
          ataque();
          checkAsesinato();
        }
        else
        {
            walk();
        } 
        super.act();
        
        
    }
    
    public void ataque() 
    { 
            if(delay==10)
            {
                if(direccion==1){
            setImage(soldierAtakRight[cont]);
            }
            else
            {
                setImage(soldierAtakLeft[cont]);
            }
            
            cont++;
            delay=0;
            
            if(cont==soldierAtakRight.length-1)
            {
                cont=0;
                ataque=false;
                pasos=0;
                direccion*=-1;
            }
        
        }
        else
        {
            delay++;
        }
    }
        // Add your action code here.
        
    public void walk()
    {
    
            
            if(delay==10)
            {
            //cont++;
             this.chacaLimite();
            setLocation(getX()+(4*direccion),getY());
            if(direccion==1)
            {
            setImage(soldierRight[cont]);
           }
           else
           {
               setImage(soldierLeft[cont]);
            }
            cont++;
            
            delay=0;
            if(cont==11)
            {
                
                cont=0;
                pasos++;
                if(pasos==totalPasos)
                {
                    ataque=true;
                    totalPasos=this.numGenerator();
                }
            }
        }
        else
        delay++;
    }
    
    private int numGenerator()
    { //Generates a random number with easy to influence min and max
        int num = 0;
        while (!(num >= 1 && num <= 5)){
            num = (int)(Math.random()*10);
        }
        return num; 
    }
    
     public void chacaLimite()
   {
       if(super.Limite())
       {
           this.cambiaDiereccion();
        }
   }
   
   public void cambiaDiereccion()
   {
       direccion=direccion*-1;
   }
   
    public void checkAsesinato()
   
   {
    
       if(((FlagWarWorld)getWorld()).getInmunidad()==false)
       {
       
       if(( getOneIntersectingObject(Body.class) != null && cont >= 9 && cont <= 13 ))
       {
           int x1 = ((FlagWarWorld)getWorld()).getXJugador() ;
           int x2 = getX();
           
           if((x2-x1>=0 && direccion<0) || (x2-x1<=0 && direccion>0) )
           {
        setLocation(((FlagWarWorld)getWorld()).getXJugador()-(25*direccion),getY());   
        //System.out.println("Muerto");   
        ((FlagWarWorld)getWorld()).setJugador(false);
        
        if(cont >= 9 && cont <= 13)
         {
             ((FlagWarWorld)getWorld()).setImageJugador(1);
             
         }
        }
        ///Greenfoot.delay(1000);
       }
   }
  }
  
  public void generaDireccion()
    {
        if(((FlagWarWorld)getWorld()).getJugador() )
        {
          int x1=getX();
          int x2=((FlagWarWorld)getWorld()).getXJugador() ;
          if(x2-x1<=0)
          {
              direccion=-1;
          }
            else
            {
                direccion=1;
            }
        
        }    
    }
}
